package com.daxt.common.config;

import com.daxt.base.common.util.RedisUtil;
import com.daxt.common.handler.CurrentUserMethodArgumentResolver;
import com.daxt.mapper.sys.OperationLogMapper;
import com.daxt.mapper.sys.UserMapper;
import com.daxt.model.dic.SystemConfigType;
import com.daxt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.*;

import java.util.List;

@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
//@ComponentScan("com.daxt.common.config")
public class WebURLConfigurer implements WebMvcConfigurer {
//	@Value("${systemConfig.ossDir}")
//    private String path;
//	@Bean
//	AuthFilter authFilter() {
//		return new AuthFilter();
//	}
//	@Value("${license}")
//    private String license;

    @Autowired
    private UserMapper userMapper;


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String ossDir = RedisUtil.getSysConfig(SystemConfigType.OSS_DIR);
        registry.addResourceHandler("/file/**").addResourceLocations("file:" + ossDir);
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/sys/**").addResourceLocations("classpath:/sys/");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/services/**");
        registry.addResourceHandler("/dwt-resources/**").addResourceLocations("classpath:/dwt-resources/");

    }



    //	授权拦截
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AuthFilter(userMapper))
                .addPathPatterns("/**");
    }


//	/**
//	 * CurrentUser 注解参数解析器
//	 *
//	 * @return
//	 */
//	@Bean
//	public CurrentUserMethodArgumentResolver currentUserMethodArgumentResolver() {
//		return new CurrentUserMethodArgumentResolver();
//	}

    //	@Autowired
//	private CurrentUserMethodArgumentResolver currentUserMethodArgumentResolver;
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new CurrentUserMethodArgumentResolver());
    }

    @Override
    @Order(0)
    public void addCorsMappings(CorsRegistry registry) {
        //设置允许跨域的路径
        registry.addMapping("/**")
                //设置允许跨域请求的域名
                .allowedOrigins("*")
                //这里：是否允许证书 不再默认开启
                .allowCredentials(true)
                //设置允许的方法
                .allowedMethods("*")
                //跨域允许时间
                .maxAge(3600);
    }


}
